package com.beself.controller;

import com.beself.entity.*;

import com.beself.service.HabitService;
import com.beself.until.RestJson;
import com.beself.until.RestJsonTwo;
import org.apache.taglibs.standard.tag.common.xml.IfTag;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.jms.Session;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/habit")
public class HabitController {



    @Resource
    private HabitService habitService;


    @RequestMapping("/newuser")
    public RestJson reg(Integer uId){

        Integer integer = habitService.addUserHabits(uId);

        if(integer<1){
            return new RestJson(false,"新增失败",0);
        }

        return new RestJson(true,"新增成功",1);


    }

    @RequestMapping("/index")
    public RestJson<Habits> findH(Integer uId){

//        Integer uid= (Integer) session.getAttribute("uId");

//        System.out.println("uid = " + uid);
//
//        System.out.println("uid = " + uid);
//        给首页返回习惯
        List<Habits> show = habitService.show(uId);


        System.out.println("show = " + show);

        return new RestJson<Habits>(true,"成功",show);
    }

//    通过习惯找任务
    @RequestMapping("/task")
    public RestJson<Tasks> findT(Integer uId,Integer hId){

        List<Tasks> tasks = habitService.showTask(uId, hId);


        return new RestJson<Tasks>(true,"成功",tasks);
    }

//    编辑任务的时间长短
    @RequestMapping("/update/tasktime")
    public RestJson modifytime(Integer tId,Integer tTimespan){

        Integer integer = habitService.updateTimespan(tId, tTimespan);

        if(integer<1){
            return new RestJson(false,"修改失败",0);
        }

        return new RestJson(true,"修改成功",1);
    }

//  删除任务
    @RequestMapping("/del/task")
    public RestJson delTask(Integer tId){

        Integer integer = habitService.delTask(tId);
        if (integer<1){

            return new RestJson(false,"删除失败",0);
        }

        return new RestJson(true,"删除成功",1);
    }
//    更改任务状态
    @RequestMapping("/update/taskState")
    public RestJson updateState(Integer tId){

        Integer integer = habitService.updateTaskstatus(tId);
        if (integer<1){

            return new RestJson(false,"更新失败",0);
        }

        return new RestJson(true,"更新成功",1);
    }

//    添加任务（已经进入早晨习惯页面）
//    先把默认任务选出来供用户选择

    @RequestMapping("/add/taskall")
    public RestJson  findDtask(Integer uId,Integer hId){

        List<DTasks> datetimes = habitService.queryAll();

        List<Tasks> tasks = habitService.showTask(uId, hId);


        return new RestJson(true,"更新成功",tasks,datetimes,1);
    }

//    点击添加按钮，添加
    @RequestMapping("/add/task")
    public RestJson insertTask(Integer uId,Integer hId,Tasks tasks){



        Integer integer = habitService.addTask(uId, hId, tasks);



        if (integer<1){

            return new RestJson(false,"添加失败",0);
        }

        return new RestJson(true,"添加成功",1);
    }

//    添加习惯
    @RequestMapping("/add/habit")
    public RestJson insertHabit(Integer uId,Habits habit){

        Integer integer = habitService.addHabit(uId, habit);

        if(integer == 0){
            return new RestJson(false,"添加习惯失败",0);
        }
        return new RestJson(true,"添加习惯成功",1);

    }
//删除习惯
//    ！！！！！！！！！！！！！！！
    @RequestMapping("/del/habit")
    public RestJson delHabit(Integer uId,Integer hId){

        Integer integer = habitService.deleteHabit(uId, hId);


        if(integer == 0){
            return new RestJson(false,"删除习惯失败",0);
        }
        return new RestJson(true,"删除习惯成功",1);
    }

//    日历查询
    @RequestMapping("/find/time")
    public RestJson findTime(Integer uId,@DateTimeFormat(pattern = "yyyy-MM-dd ")
            Date tDatetime){

        DateStatus dateStatus = habitService.queryStatus(uId, tDatetime);


        return new RestJson(true,"查询成功",dateStatus);
    }

//    补签卡
    @RequestMapping("/Retroactive")
    public RestJson updateRe(Integer uId,@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
            Date tDatetime){

        Integer integer = habitService.upDateRe(uId, tDatetime);



        if(integer == 0){
            return new RestJson(false,"补签失败",0);
        }
        return new RestJson(true,"补签成功",1);

    }

//    任务添加2
    @RequestMapping("/add/task2")
    public RestJson addTask2(String tName){

        Integer integer = habitService.addTask2(tName);

        if(integer <1){
            return new RestJson(false,"添加失败",0);
        }
        return new RestJson(true,"添加成功",1);
    }

//    日历
//    @RequestMapping("/time")
//    public RestJson time(Integer uId){
//
//        Integer i =0;
//        Integer j = 0;
//        List<Tasks> tasks = habitService.findTasks(uId);
//        System.out.println("tasks = " + tasks);
//        for (Tasks task : tasks) {
//            System.out.println("task = " + task);
//            System.out.println("task.gethId() = " + task.gethId());
//            if (task.gettStatus()==1){
//                System.out.println("任务完成");
//                i++;
//                System.out.println("i = " + i);
//            }else if (task.gettStatus()==0){
//                System.out.println("任务未完成");
//                j++;
//                System.out.println("j = " + j);
//            }
//
//        }
//        int a = i*100/tasks.size();
//        System.out.println("任务完成度："+"完成任务次数："+i+"总任务完成数："+tasks.size()+"总任务完成度："+a+"%");
//
//        return new RestJson(true,"查询成功",tasks,a);
//    }

//    时间线
    @RequestMapping("/time1")
    public Object findTasktime(Integer uId){


        Integer h =0;
        Integer l = 0;
        List<Tasks> tasks1 = habitService.findTasks(uId);
        System.out.println("tasks1 = " + tasks1);
        for (Tasks task1 : tasks1) {
            System.out.println("task1 = " + task1);
            System.out.println("task1.gethId() = " + task1.gethId());
            if (task1.gettStatus()==1){
                System.out.println("任务完成");
                h++;
                System.out.println("h = " + h);
            }else if (task1.gettStatus()==0){
                System.out.println("任务未完成");
                l++;
                System.out.println("l = " + l);
            }

        }
        int a = h*100/tasks1.size();
        System.out.println("任务完成度："+"完成任务次数："+h+"总任务完成数："+tasks1.size()+"总任务完成度："+a+"%");

        Integer i = 0;//任务完成的件数
        Integer j = 0;//任务为完成的件数

        List<Habits> show = habitService.show(uId);


        System.out.println("show = " + show);
        List habitlist = new ArrayList();
        for (Habits habit : show) {


            System.out.println("habit1 = " + habit);


            List<Tasks> tasks = habitService.showTask(uId, habit.gethId());
            System.out.println("uId = " + uId);
            System.out.println("habit.gethId() = " + habit.gethId());


            System.out.println("tasks = " + tasks);

            List<Tasks> tasksList= new ArrayList<Tasks>();
            for (Tasks task : tasks) {
                tasksList.add(task);
                System.out.println("task = " + task);
                Integer state = task.gettStatus();
                if (state==1){
                    System.out.println(habit.gethName()+" 任务完成 = " + state);
                    i++;
                }else if (state==0){
                    System.out.println(habit.gethName()+" 任务未完成 = " + state);
                    j++;
                }
            }




            habitlist.add(new RestJsonTwo<>(habit,tasksList));
        }


        return new RestJson(true,"查询成功",habitlist,a);

    }



}